package me.zhengjie.modules.inspection.service.impl;

import me.zhengjie.modules.inspection.domain.InspectionItem;
import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.repository.DictRepository;
import me.zhengjie.modules.system.service.DictDetailService;
import me.zhengjie.modules.system.service.DictService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.inspection.repository.InspectionItemRepository;
import me.zhengjie.modules.inspection.service.InspectionItemService;
import me.zhengjie.modules.inspection.service.dto.InspectionItemDTO;
import me.zhengjie.modules.inspection.service.dto.InspectionItemQueryCriteria;
import me.zhengjie.modules.inspection.service.mapper.InspectionItemMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author xuyukun
* @date 2019-07-02
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class InspectionItemServiceImpl implements InspectionItemService {

    @Autowired
    private InspectionItemRepository inspectionItemRepository;


    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private InspectionItemMapper inspectionItemMapper;

    @Override
    public Object queryAll(InspectionItemQueryCriteria criteria, Pageable pageable){
        Page<InspectionItem> page = inspectionItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Iterator<InspectionItem> itemIterator=page.iterator();
        while (itemIterator.hasNext()) {
            InspectionItem inspectionItem = itemIterator.next();
            if(inspectionItem.getObjid()!=null){
                inspectionItem.setObjname(dictDetailService.findById(Long.valueOf(inspectionItem.getObjid())).getLabel());
            }
            if(inspectionItem.getTypeid()!=null){
                if(inspectionItem.getTypeid()==1){
                    inspectionItem.setTypename("设备类巡检");
                }else if(inspectionItem.getTypeid()==2){
                    inspectionItem.setTypename("设施类巡检");
                }else if(inspectionItem.getTypeid()==3){
                    inspectionItem.setTypename("管理类巡检");
                }
            }


        }
        return PageUtil.toPage(page.map(inspectionItemMapper::toDto));


    }

    @Override
    public Object queryAll(InspectionItemQueryCriteria criteria){
        return inspectionItemMapper.toDto(inspectionItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public InspectionItemDTO findById(Long id) {
        Optional<InspectionItem> inspectionItem = inspectionItemRepository.findById(id);
        ValidationUtil.isNull(inspectionItem,"InspectionItem","id",id);
        return inspectionItemMapper.toDto(inspectionItem.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InspectionItemDTO create(InspectionItem resources) {
        return inspectionItemMapper.toDto(inspectionItemRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InspectionItem resources) {
        Optional<InspectionItem> optionalInspectionItem = inspectionItemRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalInspectionItem,"InspectionItem","id",resources.getId());

        InspectionItem inspectionItem = optionalInspectionItem.get();
        // 此处需自己修改
        resources.setId(inspectionItem.getId());
        inspectionItemRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        inspectionItemRepository.deleteById(id);
    }

    @Override
    public List<InspectionItem> findByObjectId(int dictId) {
        return inspectionItemRepository.findByObjectId(Long.valueOf(dictId));
    }

    @Override
    public List<InspectionItem> findByItemIds(List<Long> itemIds) {
        return inspectionItemRepository.findByItemIds(itemIds);
    }
}